package org.one.afternoon.controller;

import jdk.swing.interop.SwingInterOpUtils;
import org.one.afternoon.common.Result;
import org.one.afternoon.domain.JobAndTrigger;
import org.one.afternoon.domain.JobDetails;
import org.one.afternoon.domain.Movie;
import org.one.afternoon.domain.TriggerDetails;
import org.one.afternoon.quartz.QuartzBean;
import org.one.afternoon.quartz.QuartzUtils;
import org.one.afternoon.service.JobService;
import org.one.afternoon.service.TriggerService;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/quartz")
public class QuartzController {
    //注入任务调度
    @Autowired
    private Scheduler scheduler;

    @Autowired
    private TriggerService triggerService;

    @Autowired
    private JobService jobService;

    @RequestMapping("/createJob")
    @ResponseBody
    public String  createJob(QuartzBean quartzBean)  {
        try {
            //进行测试所以写死
            quartzBean.setJobClass("com.hjljy.blog.Quartz.MyTask1");
            quartzBean.setJobName("test1");
            quartzBean.setCronExpression("*/10 * * * * ?");
            QuartzUtils.createScheduleJob(scheduler,quartzBean);
        } catch (Exception e) {
            return "创建失败";
        }
        return "创建成功";
    }

    @RequestMapping("/pauseJob")
    @ResponseBody
    public String  pauseJob()  {
        try {
            QuartzUtils.pauseScheduleJob (scheduler,"test1");
        } catch (Exception e) {
            return "暂停失败";
        }
        return "暂停成功";
    }

    @RequestMapping("/runOnce")
    @ResponseBody
    public String  runOnce()  {
        try {
            QuartzUtils.runOnce (scheduler,"test1");
        } catch (Exception e) {
            return "运行一次失败";
        }
        return "运行一次成功";
    }

    @RequestMapping("/resume")
    @ResponseBody
    public String  resume()  {
        try {

            QuartzUtils.resumeScheduleJob(scheduler,"test1");
        } catch (Exception e) {
            return "启动失败";
        }
        return "启动成功";
    }

    @RequestMapping("/update")
    @ResponseBody
    public String  update(QuartzBean quartzBean)  {
        try {
            //进行测试所以写死
            quartzBean.setJobClass("com.hjljy.blog.Quartz.MyTask1");
            quartzBean.setJobName("test1");
            quartzBean.setCronExpression("10 * * * * ?");
            QuartzUtils.updateScheduleJob(scheduler,quartzBean);
        } catch (Exception e) {
            return "启动失败";
        }
        return "启动成功";
    }

    @CrossOrigin // 跨域访问
    @GetMapping("/all")
    @ResponseBody
    public Result<List<JobAndTrigger>> findAll() throws ParseException {
        List<JobAndTrigger> resultList=new ArrayList<>();
        List<TriggerDetails> triggerList = triggerService.list(null);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (TriggerDetails td: triggerList) {
            String time = sdf.format(Long.parseLong(td.getPrevFireTime()));
            td.setPrevFireTime(time);
            String time2 = sdf.format(Long.parseLong(td.getNextFireTime()));
            td.setNextFireTime(time2);
        }
        List<JobDetails> jobList = jobService.list(null);
        for(int i=0;i<jobList.size();i++){
            JobAndTrigger jobAndTrigger = new JobAndTrigger();
            jobAndTrigger.setJobName(jobList.get(i).getJobName());
            jobAndTrigger.setDescription(jobList.get(i).getDescription());
            jobAndTrigger.setNextFireTime(triggerList.get(i).getNextFireTime());
            jobAndTrigger.setPrevFireTime(triggerList.get(i).getPrevFireTime());
            resultList.add(jobAndTrigger);
        }
        return Result.success(resultList);
    }
}
